package hust.ioic.oa.service.impl;


import java.util.Collection;
import java.util.List;

import javax.annotation.Resource;

import hust.ioic.oa.service.FunctionService;
import hust.ioic.oa.base.BaseDao;
import hust.ioic.oa.base.BaseDaoImpl;
import hust.ioic.oa.domain.Function;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
@SuppressWarnings("unchecked")
public class FunctionServiceImpl extends BaseDaoImpl<Function> implements FunctionService{


	@Override
	public List<Function> findTopFunction() {
		return getSession()//
				.createQuery("FROM Function f WHERE f.parent IS NULL ORDER BY f.id ASC")//
				.list();
	}

	@Override
	public Collection<String> getAllFunctionUrls() {
		return getSession().createQuery(//
				"SELECT  f.url FROM Function f WHERE f.url IS NOT NULL ORDER BY f.id DESC")//
				.list();
	}

	@Override
	public List<Function> findChildrenFunction() {
		
		return getSession()//
				.createQuery("FROM Function f WHERE f.parent IS NOT NULL ORDER BY f.id ASC")//
				.list();
	}

	@Override
	public Function findByUrl(String url) {
		return (Function) getSession()
				.createQuery("FROM Function f WHERE f.url=? ")//
				.setParameter(0, url)
				.uniqueResult();
	}
}
